package com.k.kc.util;

import java.io.*;
import java.net.HttpURLConnection;
import java.net.URLConnection;
import java.sql.*;

/**
 * 资源关闭类
 * @author k
 */
public class Close {
    private static void closeQuietly(Closeable closeable) {
        try {
            if(closeable != null) {
                closeable.close();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    public static void close(URLConnection conn) {
        if(conn instanceof HttpURLConnection) {
            ((HttpURLConnection)conn).disconnect();
        }
    }
    public static void closeQuietly(InputStream input) {
        closeQuietly((Closeable)input);
    }
    public static void closeQuietly(OutputStream output) {
        closeQuietly((Closeable)output);
    }
    public static void closeQuietly(Reader input) {
        closeQuietly((Closeable)input);
    }
    public static void closeQuietly(Writer output) {
        closeQuietly((Closeable)output);
    }
    public static void closeQuietly(Connection conn){
        if(conn!=null){
            try {
                conn.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }
    public static void closeQuietly(ResultSet rs){
        if(rs!=null){
            try {
                rs.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }
    public static void closeQuietly(PreparedStatement pst){
        if(pst!=null){
            try {
                pst.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }
    public static void closeQuietly(Statement st){
        if(st!=null){
            try {
                st.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }
}
